#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/workqueue.h>


/*定义等待队列头*/
static DECLARE_WAIT_QUEUE_HEAD(my_wq);
static int condition =0;

static struct work_struct wrk;

static void work_handler(struct work_struct *work)
{
  printk("line = %d func=%s\n",__LINE__,__FUNCTION__);
  printk("Waitqueue module handler \n");
  msleep(5000);
  printk("Wake up this sleeping module\n");
  condition =1;
  /*唤醒等待队列*/
  wake_up_interruptible(&my_wq);

}


static int  __init my_init(void)
{
  printk("wait queue example \n");
  /*声明一个工作队列*/
  INIT_WORK(&wrk,work_handler);
  /*开始调度 在这个地方开始调度是为了防止阻塞后没有办法进行调度*/
  schedule_work(&wrk);

  printk("Going to sleep %s \n",__FUNCTION__);
  /*进入阻塞状态 等待 wake_up_interruptible 的唤醒
  condition != 0 false 进入阻塞
  */
  wait_event_interruptible(my_wq,condition != 0);

  pr_info("woken up by the work job \n");
  return 0;
}


void my_exit(void)
{
  printk("waitqueue example cleanup \n");
}

module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("jxhu <302501638@qq.com>");